﻿//-----------------------------------------------------------------------
// <copyright file="MLInvalidPersistedObject.cs" company="MAPILab Ltd">
//     Copyright (c) MAPILab Ltd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace MAPILab.SharePoint.Explorer.ObjectBrowser
{
    using System;
    using System.Data.SqlClient;
    using System.Text.RegularExpressions;
    using MAPILab.SharePoint.Explorer.Utilities;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.Upgrade;

    /// <summary>
    /// Sets instead of the object that failed to deserialize from database SharePoint
    /// </summary>
    public class MLInvalidPersistedObject
    {
        #region Fields

        /// <summary>
        /// Guid
        /// </summary>
        Guid id;

        /// <summary>
        /// Name
        /// </summary>
        string name;

        /// <summary>
        /// properties
        /// </summary>
        string properties;

        /// <summary>
        /// object type
        /// </summary>
        string objectType;

        /// <summary>
        /// regex
        /// </summary>
        static Regex objectTypeRegex = new Regex("<object type=\"(?<type>[^\"]+)\">");

        #endregion

        #region Properties

        /// <summary>
        /// gets guid
        /// </summary>
        public Guid Id
        {
            get { return id; }
        }

        /// <summary>
        /// Gets name
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        /// <summary>
        /// Gets properties
        /// </summary>
        public string Properties
        {
            get { return properties; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="id">Guid</param>
        /// <param name="name">Name</param>
        /// <param name="properties">Properties</param>
        public MLInvalidPersistedObject(Guid id, string name, string properties)
        {
            this.id = id;
            this.name = name;
            this.properties = properties;            
            this.objectType = GetObjectType(properties);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the oqject type
        /// </summary>
        /// <param name="props"></param>
        /// <returns></returns>
        static string GetObjectType(string props)
        {
            string retVal = "Unknown type";

            Match match = objectTypeRegex.Match(props);
            if (match.Success && match.Groups["type"].Success)
            { 
                retVal = match.Groups["type"].Value;
            }

            return retVal;
        }
        
        /// <summary>
        /// Override metod ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("Deserialization error:{0}", objectType);
        }

        /// <summary>
        /// Delete persisted object
        /// </summary>
        internal void Delete()
        {
            SPFarm localFarm = SPFarm.Local;
            MLSharePointSqlSession spSqlSession = new MLSharePointSqlSession(SPManager.Instance.ConnectionString.ConnectionString);
            string sqlQuery = string.Format("DELETE FROM Objects WHERE Id = '{0}'", Id.ToString());
            spSqlSession.ExecuteNonQuery(new SqlCommand(sqlQuery));
        }

        #endregion
    }
}
